home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kstringvalidator.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-10  |  4.8 KB  |  142 lines

  1. /*
  2.     kstringvalidator.h
  3.  
  4.     Copyright (c) 2001 Marc Mutz <mutz@kde.org>
  5.  
  6.     This library is free software; you can redistribute it and/or
  7.     modify it under the terms of the GNU Library General Public
  8.     License as published by the Free Software Foundation; version 2.0
  9.     of the License.
  10.  
  11.     This library is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.     Library General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU Library General Public
  17.     License along with this library; if not, write to the Free
  18.     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19.     02110-1301 USA
  20. */
  21.  
  22. #ifndef __KSTRINGVALIDATOR_H__
  23. #define __KSTRINGVALIDATOR_H__
  24.  
  25. #include <qvalidator.h>
  26. #include <qstringlist.h>
  27.  
  28. #include <kdelibs_export.h>
  29.  
  30. /**
  31.  * @short A QValidator to (dis)allow certain strings
  32.  *
  33.  * This validator allows you to accept only certain or to accept all
  34.  * but certain strings.
  35.  *
  36.  * When used in rejecting mode, accepts only strings not in the
  37.  * stringlist. This mode is the default and comes in handy when asking
  38.  * the user for a name of some listed entity. Set the list of already
  39.  * used names to prevent the user from entering duplicate names.
  40.  *
  41.  * When used in non-rejecting mode, accepts only strings that appear
  42.  * in the stringlist. Use with care! From a user's point of view this
  43.  * mode is hard to grasp.
  44.  *
  45.  * This validator can also fix strings. In rejecting mode, a number
  46.  * will be appended to the string until it is Acceptable. E.g. if
  47.  * "foo" and "foo 1" are in the stringlist, then fixup will change
  48.  * "foo" to "foo 2", provided "foo 2" isn't in the list of forbidden
  49.  * strings.
  50.  *
  51.  * In accepting mode, when the input starts with an Acceptable
  52.  * substring, truncates to the longest Acceptable string. When the
  53.  * input is the start of an Acceptable string, completes to the
  54.  * shortest Acceptable string.
  55.  *
  56.  * NOTE: fixup isn't yet implemented.
  57.  *
  58.  * @author Marc Mutz <mutz@kde.org>
  59.  **/
  60. class KDEUI_EXPORT KStringListValidator : public QValidator {
  61.   Q_OBJECT
  62.   Q_PROPERTY( QStringList stringList READ stringList WRITE setStringList )
  63.   Q_PROPERTY( bool rejecting READ isRejecting WRITE setRejecting )
  64.   Q_PROPERTY( bool fixupEnabled READ isFixupEnabled WRITE setFixupEnabled )
  65. public:
  66.   /** Construct a new validator.
  67.    *
  68.    * @param list         The list of strings to (dis)allow.
  69.    * @param rejecting    Selects the validator's mode
  70.    *                     (rejecting: true; accepting: false)
  71.    * @param fixupEnabled Selects whether to fix strings or not.
  72.    * @param parent Passed to lower level constructor.
  73.    * @param name Passed to lower level constructor
  74.    *
  75.    **/
  76.   KStringListValidator( const QStringList & list=QStringList(),
  77.             bool rejecting=true, bool fixupEnabled=false,
  78.             QObject * parent=0, const char * name=0 )
  79.     : QValidator( parent, name ), mStringList( list ),
  80.       mRejecting( rejecting ), mFixupEnabled( fixupEnabled ) {}
  81.  
  82.   virtual State validate( QString & input, int & pos ) const;
  83.   virtual void fixup( QString & input ) const;
  84.  
  85.   void setRejecting( bool rejecting ) { mRejecting = rejecting; }
  86.   bool isRejecting() const { return mRejecting; }
  87.  
  88.   void setFixupEnabled( bool fixupEnabled ) { mFixupEnabled = fixupEnabled; }
  89.   bool isFixupEnabled() const { return mFixupEnabled; }
  90.  
  91.   void setStringList( const QStringList & list ) { mStringList = list; }
  92.   QStringList stringList() const { return mStringList; }
  93.  
  94. protected:
  95.   QStringList mStringList;
  96.   bool        mRejecting;
  97.   bool        mFixupEnabled;
  98. private:
  99.   class KStringListValidator* d;
  100. };
  101.  
  102. /**
  103.  * @short A QValidator for mime types.
  104.  *
  105.  * This validator allows you to validate mimetype names
  106.  * (e.g. text/plain, image/jpeg). Note that the validation is only
  107.  * syntactically. It will e.g. not reject "foo/bar", although that
  108.  * particular mime type isn't yet registered. It suffices for the name
  109.  * to adhere to the production
  110.  *
  111.  * \code
  112.  * mime-type := token "/" token ; 'token' is defined in rfc2045
  113.  * \endcode
  114.  *
  115.  * The fixup will simply remove all non-token characters.
  116.  *
  117.  * @author Marc Mutz <mutz@kde.org>
  118.  **/
  119. class KDEUI_EXPORT KMimeTypeValidator : public QValidator
  120. {
  121.   Q_OBJECT
  122. public:
  123.   KMimeTypeValidator( QObject* parent, const char* name=0)
  124.     : QValidator( parent, name ) {}
  125.  
  126.   /**
  127.    * Checks for well-formed mimetype. Returns
  128.    * @li Acceptable iff input ~= /^[:allowed chars:]+\/[:allowed chars:]+$/
  129.    * @li Intermediate iff input ~= /^[:allowed chars:]*\/?[:allowed chars:]*$/
  130.    * @li Invalid else
  131.    */
  132.   virtual State validate( QString & input, int & pos ) const;
  133.   /**
  134.    * Removes all characters that are forbidden in mimetypes.
  135.    */
  136.   virtual void fixup( QString & input ) const;
  137. private:
  138.   class KMimeTypeValidator* d;
  139. };
  140.  
  141. #endif // __KSTRINGVALIDATOR_H__
  142.